﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace EntityCreator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        
        private void btnCreate_Click(object sender, EventArgs e)
        {
            
            StreamWriter sw = File.CreateText(string.Format("{0}.cs", this.txtClassName.Text.Trim()));

            string tableName = txtTableName.Text.Trim();
            string className = txtClassName.Text.Trim();

            StringBuilder sb = new StringBuilder();
            sb.AppendLine("using System;");
            sb.AppendLine("using LightWeightORM.ORM.MappingAttribute;\n\n");
            sb.AppendLine("namespace SmartWarehouse");
            sb.AppendLine("{");
            sb.AppendLine("namespace ORM");
            sb.AppendLine("{");
            sb.AppendLine("namespace Entity");
            sb.AppendLine("{");
            sb.AppendFormatLine("[TableMapping(\"{0}\",\"{1}\")]", tableName, primaryKey.ToLower());
            sb.AppendFormatLine("public class {0}", className);
            sb.AppendLine("{");
            sb.AppendFormatLine("public {0}()", className);
            sb.AppendLine("{");
            sb.AppendLine("}\n\n");
            foreach (DataRow row in dtFields.Rows)
            {
                string fileName = row["FieldName"].ToString();
                string dbType = row["DbType"].ToString().ToLower();
                string mapType = row["MappintType"].ToString();

                sb.AppendFormatLine("public const string _{0} = \"{1}\";", fileName.Capitalize(), fileName.ToLower());
                sb.AppendFormatLine("private {0} m_{1};", mapType, fileName.ToLower());
                sb.AppendFormatLine("[DataField(\"{0}\", \"{1}\")]", fileName.ToLower(), dbType.ToLower());
                sb.AppendFormatLine("public {0} {1}", mapType, fileName.Capitalize());
                sb.AppendLine("{");
                sb.AppendFormatLine("get {{ return m_{0}; }}", fileName.ToLower());
                sb.AppendFormatLine("set {{ m_{0} = value; }}", fileName.ToLower());
                sb.AppendLine("}\n\n");

            }
            sb.AppendLine("}");
            sb.AppendLine("}");
            sb.AppendLine("}");
            sb.AppendLine("}");

            sw.WriteLine(sb.ToString());
            sw.Flush();
            MessageBox.Show("Success !");
        }

        private DataTable dtFields = null;
        private string primaryKey = string.Empty;
        private void Form1_Load(object sender, EventArgs e)
        {
            dtFields = new DataTable();
            dtFields.Columns.Add("FieldName");
            dtFields.Columns.Add("DbType");
            dtFields.Columns.Add("MappintType");

            this.cbMappintType.SelectedIndex = 0;
            this.cbDbType.SelectedIndex = 0;
        }

        private void btnAddField_Click(object sender, EventArgs e)
        {
            if(chkPrimaryKey.Checked == true)
                this.primaryKey = this.txtFieldName.Text.Trim();

            DataRow row = dtFields.NewRow();
            row["FieldName"] = this.txtFieldName.Text.Trim();
            row["DbType"] = this.cbDbType.SelectedItem.ToString().Trim();
            row["MappintType"] = this.cbMappintType.SelectedItem.ToString().Trim();
            dtFields.Rows.Add(row);

            this.chkPrimaryKey.Checked = false;
            this.txtFieldName.Text = string.Empty;

            dataGridView1.DataSource = dtFields;
        }

        private void txtTableName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != '_')
            {
                if (e.KeyChar == '\b' && this.txtClassName.Text != string.Empty)
                    this.txtClassName.Text = this.txtClassName.Text.Substring(0, this.txtClassName.Text.Length - 1);
                else
                    this.txtClassName.Text += e.KeyChar.ToString();

            }
                
        }

        private void txtClassName_KeyPress(object sender, KeyPressEventArgs e)
        {

        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            this.dtFields.Clear();
            this.dataGridView1.DataSource = dtFields;
        } 
    }
    public static class MyExtensions
    {

        public static string Capitalize(this string str)
        {
            return string.Format("{0}{1}", str.Substring(0, 1).ToUpper(), str.Substring(1, str.Length - 1).ToLower());
        }
        public static void AppendFormatLine(this StringBuilder sb, string format, params object[] args)
        {
            sb.AppendFormat(format, args);
            sb.Append("\n");
        }
    }  
}
